export const override_fragment = `
varying float color;

vec4 encodeFloat2RGBA(float v)
{
    vec4 enc=vec4(1.,255.,65025.,16581375.)*v;
    enc=fract(enc);
    enc-=enc.yzww*vec4(1./255.,1./255.,1./255.,0.);
    return enc;
}
void main(){
    gl_FragColor=encodeFloat2RGBA(1.-color);
}
`

export const override_vertex = `
varying float color;

void main(){
    gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.);
    color=gl_Position.z/2.+.5;
}
`
export const vertex = `
attribute vec3 pos;
uniform float top;
uniform float bottom;
uniform float time;
uniform mat4 cameraMatrix;
varying float depth;
varying vec2 depthUv;
#include <common>
float angle(float x,float y){
    return atan(y,x);
}
vec2 getFoot(vec2 camera,vec2 _n_pos,vec2 pos){
    vec2 position;
    
    float distanceLen=distance(pos,_n_pos);
    
    float a=angle(camera.x-_n_pos.x,camera.y-_n_pos.y);
    
    pos.x>_n_pos.x?a-=.785:a+=.785;
    
    position.x=cos(a)*distanceLen;
    position.y=sin(a)*distanceLen;
    
    return position+_n_pos;
}

#include <batching_pars_vertex>
#include <uv_pars_vertex>
#include <envmap_pars_vertex>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main(){
    #include <uv_vertex>
    #include <color_vertex>
    #include <morphinstance_vertex>
    #include <morphcolor_vertex>
    #include <batching_vertex>
    #if defined(USE_ENVMAP)||defined(USE_SKINNING)
    #include <beginnormal_vertex>
    #include <morphnormal_vertex>
    #include <skinbase_vertex>
    #include <skinnormal_vertex>
    #include <defaultnormal_vertex>
    #endif
    
    float height=top-bottom;
    
    vec3 _n_pos=vec3(pos.x,pos.y-height/30.,pos.z);
    
    vec2 foot=getFoot(vec2(cameraPosition.x,cameraPosition.z),vec2(_n_pos.x,_n_pos.z),vec2(position.x,position.z));
    float y=_n_pos.y-bottom-height*fract(time);
    y+=y<0.?height:0.;
    
    depth=(1.-y/height);
    
    y+=bottom;
    y+=position.y-_n_pos.y;
    vec3 transformed=vec3(foot.x,y,foot.y);
    vec4 cameraDepth=cameraMatrix*vec4(transformed,1.);
    depthUv=cameraDepth.xy/2.+.5;
    
    #include <morphtarget_vertex>
    #include <skinning_vertex>
    #include <project_vertex>
    #include <logdepthbuf_vertex>
    #include <clipping_planes_vertex>
    #include <worldpos_vertex>
    #include <envmap_vertex>
    #include <fog_vertex>
}
`

export const fragment = `
uniform vec3 diffuse;

uniform sampler2D tDepth;
uniform float opacity;
varying float depth;
varying vec2 depthUv;

float decodeRGBA2Float(vec4 rgba)
{
    return dot(rgba,vec4(1.,1./255.,1./65025.,1./16581375.));
}

#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#include <common>
#include <dithering_pars_fragment>
#include <color_pars_fragment>
#include <uv_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <alphahash_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <envmap_common_pars_fragment>
#include <envmap_pars_fragment>
#include <fog_pars_fragment>
#include <specularmap_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>
void main(){
    
    if(1.-depth<decodeRGBA2Float(texture2D(tDepth,depthUv)))discard;
    vec4 diffuseColor=vec4(diffuse,opacity);
    
    #include <clipping_planes_fragment>
    #include <logdepthbuf_fragment>
    #include <map_fragment>
    #include <color_fragment>
    #include <alphamap_fragment>
    #include <alphatest_fragment>
    #include <alphahash_fragment>
    #include <specularmap_fragment>
    ReflectedLight reflectedLight=ReflectedLight(vec3(0.),vec3(0.),vec3(0.),vec3(0.));
    #ifdef USE_LIGHTMAP
    vec4 lightMapTexel=texture2D(lightMap,vLightMapUv);
    reflectedLight.indirectDiffuse+=lightMapTexel.rgb*lightMapIntensity*RECIPROCAL_PI;
    #else
    reflectedLight.indirectDiffuse+=vec3(1.);
    #endif
    #include <aomap_fragment>
    reflectedLight.indirectDiffuse*=diffuseColor.rgb;
    vec3 outgoingLight=reflectedLight.indirectDiffuse;
    #include <envmap_fragment>
    #include <opaque_fragment>
    #include <tonemapping_fragment>
    #include <colorspace_fragment>
    #include <fog_fragment>
    #include <premultiplied_alpha_fragment>
    #include <dithering_fragment>
}
`